﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
using DXBBS.Components;
using DXBBS.Business;
using DXBBS.Service;
using System.Reflection;
using System.Web.Caching;


namespace WebApp
{
    public class PageBase1 : Page
    {

        private xUserManager m_UserManager = new xUserManager();
        private RoleManager m_RoleManager = new RoleManager();
        private ForumManager m_ForumManager = new ForumManager();

        private   xuser m_SignInUser;
        public    xuser SignInUser
        {
            get
            {
                if (null == this.m_SignInUser)
                {
                    if (null != Session["xSignInUser"])
                    {
                        this.m_SignInUser = Session["xSignInUser"] as xuser;
                    }
                }
                return this.m_SignInUser;
            }
            set
            {
                Session["xSignInUser"] = value;
                this.m_SignInUser = value;
            }
        }

        public void SignOut()
        {
            //logout from bbs
            if (Sessions.UserID != 0)
            {
                Hashtable hashtable = (Hashtable)Caches.Read(CacheKey.OnlineList);
                if ((hashtable != null) && (hashtable[Sessions.UserID.ToString()] != null))
                {
                    hashtable.Remove(Sessions.UserID.ToString());
                }
            }

            if (Cosmetic.WebGeneric.ReadBBSConfigInfo().Interface == 1)
            {
                try
                {
                    ((IUser)Assembly.Load("DxbbsUserClass").CreateInstance("DxbbsUserClass.Interface")).outLogout(Sessions.UserName);
                }
                catch
                {
                }
            }
            Sessions.Abandon();
            Session.Abandon();
            Session.Remove("xSignInUser");
        }


        public bool CheckUserRole(RoleManager.ERole aRole)
        {
            if (null == this.SignInUser)
            {
                return false;
            }
            return this.m_RoleManager.CheckUserRole(this.m_SignInUser.id , aRole);
        }

        public bool CheckForumAdmin(Guid aForumId)
        {
            if (null == this.SignInUser)
            {
                return false;
            }
            return this.m_ForumManager.CheckForumAdmin(aForumId, this.SignInUser.id );
        }
    }
}